﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LTHD_Web.WS;

namespace LTHD_Web
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

            if (!IsPostBack)
            {
                Service1SoapClient ws = new Service1SoapClient();
                List<DiaDiemDTO> diadiemList = new List<DiaDiemDTO>();
                //neu nguoi dung chua dang nhap, tao ra treeview binh thuong
                if (Request.Cookies["userName"] == null)
                {
                    diadiemList = ws.getAllDiaDiem();
                    if (diadiemList != null)
                    {
                        TreeNode root = new TreeNode();
                        root.NavigateUrl = "#";
                        root.Selected = true;
                        root.Text = "Danh sách địa điểm du lịch";
                        root.Value = "0";
                        createRoot(ref root, diadiemList);
                        tvDanhSachDD.Nodes.Add(root);
                    }
                }
                //Neu nguoi dung da dang nhap
                if (Request.Cookies["userName"] != null)
                {
                    lb_taikhoan.Text =  Request.Cookies["userName"].Value;
                    diadiemList = ws.getAllDiaDiem();
                    if (diadiemList != null)
                    {
                        TreeNode root = new TreeNode();
                        root.NavigateUrl = "#";
                        root.Selected = true;
                        root.Text = "Danh sách địa điểm du lịch";
                        root.Value = "0";
                        createRoot(ref root, diadiemList);
                        tvDanhSachDD.Nodes.Add(root);
                    }
                    //Thêm các địa điểm vào TreeView------
                    string _username = Server.HtmlEncode(Request.Cookies["userName"].Value).ToString();
                    TaiKhoanDTO tk = ws.getAccountByTen(_username);
                    diadiemList = ws.getDiaDiemByAccount(tk.Id, 0);
                    if (diadiemList != null)
                    {
                        TreeNode root = new TreeNode();
                        root.NavigateUrl = "#";
                        root.Selected = true;
                        root.Text = "Danh sách địa điểm của bạn";
                        root.Value = "1";
                        createRoot(ref root, diadiemList);
                        tvDanhSachDD.Nodes.Add(root);
                    }
                    diadiemList = ws.getDiaDiemByAccount(tk.Id, 1);
                    if (diadiemList != null)
                    {
                        TreeNode root = new TreeNode();
                        root.NavigateUrl = "#";
                        root.Selected = true;
                        root.Text = "Danh sách địa điểm được share";
                        root.Value = "0";
                        createRoot(ref root, diadiemList);
                        tvDanhSachDD.Nodes.Add(root);
                    }
                }
            }
        }
        private void createRoot(ref TreeNode root, List<DiaDiemDTO> diadiemList)
        {
            if (diadiemList == null)
                return;
            foreach (DiaDiemDTO diaDiem in diadiemList)
            {
                TreeNode newNode = new TreeNode();
                newNode.NavigateUrl = "#";
                string text = "<div id = '" + diaDiem.Id + "' onclick=\"movemaps(17,'" + diaDiem.Ten + "', " + diaDiem.Id + ", " + diaDiem.Latitude + ", " + diaDiem.Longitude + ", 1 , 0 )\">" + diaDiem.Ten + "</div>";
                newNode.Text = text;
                newNode.NavigateUrl = "#";
                root.ChildNodes.Add(newNode);
            }
        }
    }
}